package com.yanqu.road.server.cmd.relatives;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.relatives.RelativesProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import io.netty.channel.ChannelHandlerContext;

@Cmd(code = Protocol.S_RELATIVES_GET_USER_INFO_FROM_CROSS, desc = "拿玩家基础数据")
public class GetUserBaseInfoFromCrossCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage yanQuMessage) throws Exception {

        RelativesProto.CrossGetUserBaseInfoReqMsg reqMsg = RelativesProto.CrossGetUserBaseInfoReqMsg.parseFrom(yanQuMessage.getBytes());

        RelativesProto.CrossGetUserBaseInfoRespMsg.Builder respMsg = RelativesProto.CrossGetUserBaseInfoRespMsg.newBuilder();
        respMsg.setServerId(reqMsg.getFromServerId());
        long serverId = GameServer.getInstance().getServerId();
        for (long userId : reqMsg.getUserIdList()) {
            UserBaseInfo userBaseInfo = UserMgr.getUserBaseInfo(userId, serverId);
            if(userBaseInfo != null){
                RelativesProto.CrossUserBaseInfoTemp.Builder builder = RelativesProto.CrossUserBaseInfoTemp.newBuilder();
                builder.setUserId(userId);
                builder.setBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
                respMsg.addUser(builder);
            }
        }
        GamePlayerMgr.sendPacket(0, YanQuMessageUtils.buildMessage(Protocol.C_RELATIVES_GET_USER_INFO_BACK, respMsg));
    }

}
